<?php

// +----------------------------------------------------------------------
// | 一些普通的模型
// +----------------------------------------------------------------------

namespace Weixin\Model;

use Common\Model\Model;

class IndexModel extends Model {

    /**
     * 留言模型
     * @param $post 提交的数据
     * @param $type 留言的模块(有作品,有文章类的)
     * @return bool
     */
   public function sendMessage($post,$type){
       $model = M('UserMessage');
       if(empty($post)){
           $this->error='提交数据出错,请重试!';
           return false;
       }
       $post['type'] = $type;
       $post['create_time'] = time();
       $post['status']=true;
       if ($model->create($post)) {
           $result = $model->add();
           if ($result) {
               return $result;
           }else{
               $this->error = '留言失败,请稍候再试！';
               return false;
           }
       } else {
           return false;
       }
   }
   
   /**
    * 关注模型
    * 如果status=false表示曾经关注,但取消了关注
    * @param $post
    * @return bool
    */
   public function addfans($post){
       $model=M('UserFans');
       if(empty($post)){
           $this->error='关注提交出错,请稍候再试!';
           return false;
       }
       if(empty($post['userid'])){
           $this->error='参数错误,关注失败!';
           return false;
       }
       $post['create_time']=time();
       $post['status']=true;
       if($model->where(array('userid'=>$post['userid'],'status'=>true,'myid'=>$post['myid']))->count()>0){
           $this->error = '该焙友已存在您的关注表中!';
           return false;
       }
   
       $fansid=$model->where(array('userid'=>$post['userid'],'status'=>false,'myid'=>$post['myid']))->getField('id');
       if($fansid) {
           $model->where(array('id'=>$fansid))->save($post);
           M('User')->where(array('userid'=>$post['myid']))->setInc('attentionnum',1);
           M('User')->where(array('userid'=>$post['userid']))->setInc('fansnum',1);
           return true;
       }
       if ($model->create($post)) {
           $result = $model->add();
           if ($result) {
               M('User')->where(array('userid'=>$post['myid']))->setInc('attentionnum',1);
               M('User')->where(array('userid'=>$post['userid']))->setInc('fansnum',1);
               return true;
           }else{
               $this->error = '关注失败,请稍候再试！';
               return false;
           }
       } else {
           return false;
       }
   }
   
   /**
    * 获取食谱分类
    * @param unknown $parentid
    * @param string $type
    * @return Ambigous <boolean, unknown>
    */
   public function getCategory($parentid, $type = true)
   {
       $list = M('Category')->where(array('parentid' => $parentid, 'status' => 1))->order('sort desc')->select();
       if($type)
       {
           if($list)
           {
               foreach($list as $k => $v)
               {
                   $children = $this->getCategory($v['cateid'], false);
                   $list[$k]['childNum'] = count($children);
                   if($v['parentid'] == 0)
                   {
                       $list[$k]['icon'] = M('WeixinCategory')->where(array('cateid' => $v['cateid']))->getField('image');
                   }
                   if(count($children) > 0)
                   {
                       $num = 0;
                       $arr = array();
                       foreach($children as $ck => $cv)
                       {
                           $arr[$num][] = $cv;
                           if(($ck + 1) % 3 == 0)
                           {
                               $num++;
                           }
                       }
                       $list[$k]['childlist'] = $arr;
                   }
               }
           }
       }
       return $list;
   }
   
   /**
    * 添加收藏模型
    * @param $post
    * @param $type
    * @return bool
    */
   public function addfavorite($post,$type){
       $model=M('UserFavorite');
       if(empty($post)){
           $this->error='点赞提交出错,请稍候再试!';
           return false;
       }
       $post['type']=$type;
       $post['create_time']=time();
       $post['status']=true;
       $fvid=$model->where(array('userid'=>$post['userid'],'status'=>false,'infoid'=>$post['infoid']))->getField('id');
       if($fvid) {
           $model->where(array('id'=>$fvid))->save($post);
           M('Works')->where(array('workid'=>$post['infoid']))->setInc('favoritenum',1);
           return true;
       }
       if ($model->create($post)) {
           $result = $model->add();
           if ($result) {
               M('Works')->where(array('workid'=>$post['infoid']))->setInc('favoritenum',1);
               return true;
           }else{
               $this->error = '收藏失败,请稍候再试！';
               return false;
           }
       } else {
           return false;
       }
   }
   
   /**
    * 点赞
    * @param $post
    * @param $type 点赞类型
    * @return bool
    */
   
   public function adddig($post,$type){
       $model=M('Dig');
       if(empty($post)){
           $this->error='点赞提交出错,请稍候再试!';
           return false;
       }
       $post['type']=$type;
       $post['create_time']=time();
       $post['status']=true;
       // $post['digtype']=empty($post['digtype'])?1:0;
       if(!isset($_GET['digtype'])){
           $post['digtype']=1;
       }
       if($model->where(array('userid'=>$post['userid'],'status'=>true,'infoid'=>$post['infoid']))->count()>0){
           $this->error='您已点赞过,感谢您的支持!';
           return false;
       }
       $digs=$model->field('id,digtype')->where(array('userid'=>$post['userid'],'status'=>false,'infoid'=>$post['infoid']))->find();
   
       if($digs) {
           $model->where(array('id'=>$digs['id']))->save($post);
           if($post['digtype']==1){
               M('Works')->where(array('workid'=>$post['infoid']))->setInc('goodnum',1);
           }elseif($post['digtype']==0){
               M('Works')->where(array('workid'=>$post['infoid']))->setInc('badnum',1);
           }
           return true;
       }else{
   
           if ($model->create($post)) {
               $result = $model->add();
               if ($result) {
                   if ($post['digtype'] == 1) {
                       M('Works')->where(array('workid' => $post['infoid']))->setInc('goodnum', 1);
                   } elseif ($post['digtype'] == 0) {
                       M('Works')->where(array('workid' => $post['infoid']))->setInc('badnum', 1);
                   }
                   return true;
               } else {
                   $this->error = '点赞失败,请稍候再试！';
                   return false;
               }
           } else {
               return false;
           }
       }
   }
   
   /**
    * 分享达人排行
    * @param $num
    * @return mixed
    */
   public function sharedaren($num, $myuid){
       $where['status']=array('EQ',true);
       $data=M('User')->where($where)->field('userid,nickname,uavatar,worksnum,attentionnum,fansnum,openuid')->order('worksnum DESC,fansnum DESC')->limit($num)->select();
       foreach($data as $key=>$value){
           if(M('UserFans')->where(array('userid'=>$value['userid'],'myid'=>$myuid,'status'=>true))->count()>0){
               $data[$key]['isfans']=true;
           }
       }
       return $data;
   }
   
   /**
    * 首页食谱
    * @param number $num
    * @return unknown
    */
   public function getWorkLight($num = 2)
   {
       $data = M('Works')->where(array('status' => 1, 'iftj' => 1))->order('clicknum desc')->limit($num)->select();
       if(!$data)
       {
           $data = M('Works')->where(array('status' => 1, 'ifindex' => 1))->order('clicknum desc')->limit($num)->select();
       }
       return $data;
   }
   
   /**
    * 首页随拍
    * @param number $num
    * @return unknown
    */
   public function getSuipaiLight($num = 3)
   {
       $data = M('Suipai')->where(array('status' => 1))->order('create_time desc')->limit($num)->select();
       return $data;
   }
   
   /**
    * 获取热搜类别
    * @return Ambigous <multitype:, unknown>
    */
   public function getLightCate()
   {
       //$data = M('category')->where(array('status' => 1, 'iflight' => 1))->order('sort desc')->select();
       $data = M('WeixinSearch')->where(array('status' => 1))->order('sort desc')->select();
       $arr = array();
       if($data)
       {
           $num = 0;
           foreach($data as $k => $v)
           {
               $arr[$num][] = $v;
               if(($k + 1) % 4 == 0)
               {
                   $num++;
               }
           }
       }
       return $arr;
   }
   
   /**
    * 获取广告
    * @param $menu 所属菜单
    * @param $position 位置
    * @param $from 从第几个开始
    * @param int $num 数量
    * @return mixed 返回数据/数组
    */
   public function getAdvert($position,$from,$num=1){
       $adModel=M('WeixinAd');
       $where['status']=array('EQ',true);
       $where['position']=array('EQ',$position);
       $field=array('image,linkurl,title');
       if($num<2){
           $ad=$adModel->where($where)->field($field)->order('sort DESC,id DESC')->limit($from,$num)->find();
       }else{
           $ad=$adModel->where($where)->field($field)->order('sort DESC,id DESC')->limit($from,$num)->select();
       }
       return $ad;
   }
   
   /**
    * 获取导航
    * @param number $limit
    */
   public function getNav($limit = 5)
   {
       $data = M('WeixinNav')->where(array('status' => 1))->order('sort desc')->limit($limit)->select();
       return $data;
   }
   
   /**
    * 获取活动信息
    */
   public function getActivity()
   {
       $data = M('WeixinActivity')->select();
       $arr = array();
       if($data)
       {
           foreach($data as $k => $v)
           {
               $arr[$v['actkey']] = $v['actval'];
           }
       }
       return $arr;
   }
   
}